package com.efounder.form.comp;

import android.content.Context;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;

import com.efounder.builder.base.data.DataSetEvent;
import com.efounder.builder.base.data.DataSetListener;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.form.DMColComponent;
import com.efounder.form.DMComponent;
import com.efounder.form.DataSetComponent;
import com.efounder.form.base.IComponent;
import com.efounder.form.util.FormCompUtil;
import com.efounder.frame.ViewSize;
import com.efounder.frame.utils.EFFrameUtils;
import com.efounder.mobilecomps.R;
import com.mixiaoxiao.smoothcompoundbutton.SmoothCheckBox;
import com.mixiaoxiao.smoothcompoundbutton.SmoothCompoundButton;

/**
 * Created by lch on 16/9/2.
 * 单选框
 */
public class FormCheckBox extends SmoothCheckBox implements IComponent,IUIComponent,DMComponent,DMColComponent,DataSetListener {

	private int gap = 0;
	private ViewSize size = new ViewSize(0,0);

	private String dataSetID;
	private String dataSetColID;

	private String horizontalAlign;
	private String verticalAlign;

	private DataSetComponent dataSetComponent;
	private EFDataSet dataSet;
	private EFRowSet mainRowSet;

	private IUIComponent parentComp;


	private String currentSelectedItem;

	public FormCheckBox() {
		super(EFFrameUtils.getCurrentActivity());
	}

	public FormCheckBox(Context context) {
		super(context);
	}
	public FormCheckBox(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void dataChanged(EFDataSet dataSet) {
		mainRowSet = dataSet.getCurrentRowSet();
		String value = "";
		if (mainRowSet != null){
			value = mainRowSet.getString(dataSetColID, "");
		}
		setText(value);
		setChecked(mainRowSet.isRowSelected());
	}
	@Override
	public DataSetComponent getDataSetComponent() {
		return dataSetComponent;
	}

	@Override
	public void setDataSetComponent(DataSetComponent dsc) {
		if (dataSetComponent != null)
			dataSetComponent.removeDMComponent(this);
		dataSetComponent = dsc;
		if (dataSetComponent != null)
			dataSetComponent.insertDMComponent(this);
	}

	@Override
	public void setLayout() {
		FormCompUtil.setLayoutSize(this);
		this.setGravity(FormCompUtil.getLayoutGravity(horizontalAlign, verticalAlign));
		this.setGravity(Gravity.CENTER);
	}

	@Override
	public void creationComplete() {


		this.setEnabled(true);
		this.setClickable(true);
		//微调下样式
//		LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) this.getLayoutParams();
//		layoutParams.leftMargin=20;
//		this.setLayoutParams(layoutParams);
		this.setPadding(0,0,0,0);

		this.setWidth(getCompWidth());
		this.setHeight(getCompHeight());
		this.setEllipsize(TruncateAt.END);
		//this.setSingleLine(true);
		this.setBackgroundResource(android.R.color.white);
		this.setTextColor(getResources().getColor(R.color.black));
		this.setText(getLabel());

		this.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(SmoothCompoundButton smoothCompoundButton, boolean b) {
				if (mainRowSet != null) {
					mainRowSet.setRowSelected(b);
				}
			}
		});


	}

	public String getHorizontalAlign() {
		return horizontalAlign;
	}

	public void setHorizontalAlign(String horizontalAlign) {
		this.horizontalAlign = horizontalAlign;
	}

	public String getVerticalAlign() {
		return verticalAlign;
	}

	public void setVerticalAlign(String verticalAlign) {
		this.verticalAlign = verticalAlign;
	}

	@Override
	public void setDataSetID(String dataSetID) {
		this.dataSetID = dataSetID;
	}

	@Override
	public String getDataSetColID() {
		return dataSetColID;
	}

	@Override
	public void setDataSetColID(String dataSetColID) {
		this.dataSetColID = dataSetColID;
	}

	@Override
	public String getDataSetNameColID() {
		return null;
	}

	@Override
	public void setDataSetNameColID(String value) {

	}

	@Override
	public String getInternalDataSetID() {
		return null;
	}

	@Override
	public void setInternalDataSetID(String dataSetID) {

	}

	@Override
	public void setViewDataSetID(String viewDataSetID) {

	}

	@Override
	public String getViewDataSetID() {
		return null;
	}

	@Override
	public void setViewDataSetColID(String viewDataSetColID) {

	}

	@Override
	public String getViewDataSetColID() {
		return null;
	}

	@Override
	public void setValueDataSetColID(String valueDataSetColID) {

	}

	@Override
	public String getValueDataSetColID() {
		return null;
	}

	@Override
	public Boolean getIsUserInternalDataSetID() {
		return null;
	}

	@Override
	public void setIsUserInternalDataSetID(Boolean v) {

	}

	@Override
	public String getIdentifier() {
		return null;
	}

	@Override
	public Object getPropertyMap() {
		return null;
	}

	@Override
	public void setPropertyMap(Object value) {

	}

	@Override
	public Object getProperty(String key, Object defVal) {
		return null;
	}

	@Override
	public void setProperty(String key, Object value) {

	}

	@Override
	public int getNumberPrecision() {
		return 0;
	}

	@Override
	public void seNumberPrecision(int value) {

	}

	@Override
	public String getNumberFormat() {
		return null;
	}

	@Override
	public void setNumberFormat(String numberFormat) {

	}

	@Override
	public String getColumnType() {
		return null;
	}

	@Override
	public void setColumnType(String value) {

	}

	@Override
	public String getEditorType() {
		return null;
	}

	@Override
	public void setEditorType(String value) {

	}
	@Override
	public String getDateFormat() {
		return null;
	}

	@Override
	public void setDateFormat(String dateFormat) {

	}

	@Override
	public String getFormulaOne() {
		return null;
	}

	@Override
	public void setFormulaOne(String formulaOne) {

	}



	@Override
	public String getFkeyID() {
		return null;
	}

	@Override
	public void setFkeyID(String fkey) {

	}

	@Override
	public String getRlglID() {
		return null;
	}

	@Override
	public void setRlglID(String rlglID) {

	}

	@Override
	public String getDataSetID() {
		return dataSetID;
	}

	@Override
	public void setDataSet(EFDataSet dataSet) {
		if ( this.dataSet != dataSet ) {
			if ( this.dataSet != null ) this.dataSet.removeDataSetListener(this);
			this.dataSet = dataSet;
			if ( this.dataSet != null ) this.dataSet.addDataSetListener(this);
			this.dataChanged(dataSet);
		}

	}

	@Override
	public EFDataSet getDataSet() {
		return dataSet;
	}

	@Override
	public EFRowSet getMainRowSet() {
		return mainRowSet;
	}

	@Override
	public void dataSetChanged(DataSetEvent event) {
		dataChanged(event.getDataSet());
	}

	@Override
	public String getID() {
		return id;
	}

	@Override
	public void setID(String id) {
		this.id = id;
	}

	@Override
	public int getGap() {
		return gap;
	}

	@Override
	public void setGap(int gap) {
		this.gap = gap;
	}


	@Override
	public ViewSize getViewSize() {
		return size;
	}

	@Override
	public void setViewSize(ViewSize size) {
		this.size = size;
	}

	@Override
	public String getEditMask() {
		return null;
	}

	@Override
	public void setEditMask(String editMask) {
	}
	@Override
	public void setCompWidth(int width) {
		this.width = width;
	}

	@Override
	public int getCompWidth(){
		return this.width;
	}

	@Override
	public void setCompHeight(int height) {
		this.height = height;
	}

	@Override
	public int getCompHeight() {
		return this.height;
	}

	//-------------------------------------------------------
	// 基本属性
	//-------------------------------------------------------
	private String id;
	private float x = 0;
	private float y = 0;
	private int width;
	private int height;
	private int percentWidth;
	private int percentHeight;
	private Boolean visible;
	private int tabIndex = 0;

	private String label;
	private String selValue;
	private String deSelValue;
	private Boolean canEdit;
	private int fontColor = 0;
	private int fontSize = 12;
	private int borderColor = 0;
	private int backgroundColor = 0;
	private int symbolColor = 0;
	private String toolTip;


	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}
	public void setY(float y) {
		this.y = y;
	}
	public void setWidth(int width) {
		this.width = width;
		super.setWidth(width);
	}
	public void setHeight(int height) {
		this.height = height;
		super.setHeight(height);
	}


	public int getPercentWidth() {
		return percentWidth;
	}

	public void setPercentWidth(int percentWidth) {
		this.percentWidth = percentWidth;
	}

	public int getPercentHeight() {
		return percentHeight;
	}

	public void setPercentHeight(int percentHeight) {
		this.percentHeight = percentHeight;
	}



	public Boolean getVisible() {
		return visible;
	}
	public void setVisible(Boolean visible) {
		this.visible = visible;
	}


	public String getLabel() {
		return label;
	}
	public void setLabel(String label) {
		this.label = label;
		this.setText(label);
	}

	public String getToolTip(String a) {
		return toolTip;
	}
	public void setToolTip(String toolTip) {
		this.toolTip = toolTip;
	}


	public int getTabIndex() {
		return tabIndex;
	}
	public void setTabIndex(int tabIndex) {
		if(tabIndex != -1) {
			this.tabIndex = tabIndex;
		}
	}

	public String getSelValue() {
		return selValue;
	}
	public void setSelValue(String selValue) {
		this.selValue = selValue;
	}
	public String getDeSelValue() {
		return deSelValue;
	}
	public void setDeSelValue(String deSelValue) {
		this.deSelValue = deSelValue;
	}
	public Boolean getCanEdit() {
		return canEdit;
	}
	public void setCanEdit(Boolean canEdit) {
		this.canEdit = canEdit;
		super.setEnabled(canEdit);
	}
	public int getFontColor() {
		return fontColor;
	}
	public void setFontColor(int fontColor) {
		this.fontColor = fontColor;
	}
	public int getFontSize() {
		return fontSize;
	}
	public void setFontSize(int fontSize) {
		this.fontSize = fontSize;
	}
	public int getBorderColor() {
		return borderColor;
	}
	public void setBorderColor(int borderColor) {
		this.borderColor = borderColor;
	}
	public int getBackgroundColor() {
		return backgroundColor;
	}
	public void setBackgroundColor(int backgroundColor) {
		this.backgroundColor = backgroundColor;
	}
	public int getSymbolColor() {
		return symbolColor;
	}
	public void setSymbolColor(int symbolColor) {
		this.symbolColor = symbolColor;
	}


	@Override
	public IUIComponent getParentComp() {
		return parentComp;
	}

	@Override
	public void setParentComp(IUIComponent comp) {
		parentComp = comp;
	}

}